package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"user/config"
	"user/global"
	"user/inits"
	"user/internal/queue"
	"user/ws"
)

func main() {
	config.GetViperData()
	inits.MysqlInit()
	inits.InitRabbitMq()
	go Message()
	r := gin.Default()
	ws.LoadRouter(r)
	r.Run(":8081") // 监听并在 0.0.0.0:8080 上启动服务
}

func Message() {
	msgs := queue.PublishSub("Register")

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			for _, conn := range global.OnlineUser {
				conn.WriteMessage(1, d.Body)
			}
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
